package com.rrjjg.app.admin.api;

import com.rrjjg.app.admin.api.vo.RoleVo;
import com.rrjjg.app.admin.api.vo.UserVo;
import com.rrjjg.app.admin.bus.BRoleService;
import com.rrjjg.app.admin.bus.bo.PlatformBo;
import com.rrjjg.app.admin.bus.bo.UserBo;
import com.rrjjg.app.admin.data.storage.entity.Platform;
import com.rrjjg.app.admin.data.storage.entity.Role;
import com.rrjjg.app.admin.data.storage.entity.RolePro;
import com.rrjjg.app.admin.data.storage.entity.User;
import com.rrjjg.app.core.beans.ResultData;
import com.rrjjg.app.core.utils.ValidationUtil;
import com.rrjjg.app.mybatis.bean.PageData;
import com.rrjjg.app.mybatis.bean.PageVo;
import com.rrjjg.app.mybatis.util.PageUtil;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

import static com.rrjjg.app.admin.module.Constants.*;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author vayne
 * @since 2023-04-06
 */
@RestController
@RequestMapping("/rrjjg/user")
public class UserRoleController {

    @Autowired
    BRoleService bRoleService;


    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public ResultData<Boolean> addUser(@RequestBody UserVo vo) {
        ValidationUtil.validation(vo);
        boolean save = bRoleService.getiUserService().save(PageUtil.transformation(vo,User::new));
        return new ResultData<>(save);
    }

    @RequestMapping(value = "/delUser", method = RequestMethod.POST)
    public ResultData<Boolean> delUser(@RequestBody UserVo vo) {
        ValidationUtil.validation(vo, ValidationUtil.del.class);
        boolean save = bRoleService.getiUserService().removeById(vo.getId());
        return new ResultData<>(save);
    }

    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public ResultData<Boolean> updateUser(@RequestBody UserVo vo) {
        ValidationUtil.validation(vo, ValidationUtil.update.class);
        boolean save = bRoleService.getiUserService().updateById(PageUtil.transformation(vo,User::new));
        return new ResultData<>(save);
    }

    @RequestMapping(value = "/listUser", method = RequestMethod.POST)
    public ResultData<PageData<UserBo>> listUser(@RequestBody PageVo<UserVo> vo) {
        UserVo data = vo.getData();
        PageData<UserBo> ans = bRoleService.getiUserService().queryPage(PageUtil.transformation(data, User::new), vo.getPage(), vo.getSize());
        return new ResultData<>(ans);
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResultData<String> login(@RequestBody UserVo vo) {
        ValidationUtil.validation(vo, ValidationUtil.add.class);
        String login = bRoleService.getiUserService().login(PageUtil.transformation(vo, User::new));
        return new ResultData<>(login);
    }

    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public ResultData<UserVo> info() {
        val cache = bRoleService.getiUserService().info();
        UserVo userVo = new UserVo();
        int id = (int) cache.get(CONFIG_TOKEN_BODY_ID);
        userVo.setId(Long.valueOf(id))
                .setRoles(Arrays.asList((String) cache.get(CONFIG_TOKEN_BODY_ROLE)))
                .setLoginName((String) cache.get(CONFIG_TOKEN_BODY_NAME));
        return new ResultData<>(userVo);
    }


    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public ResultData<User> logout() {
        return new ResultData<>(null);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultData<Boolean> add(@RequestBody RoleVo vo) {
        ValidationUtil.validation(vo);
        boolean b = bRoleService.addRole(PageUtil.transformation(vo, Role::new), vo.getTree());
        return new ResultData<>(b);
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ResultData<Boolean> del(@RequestBody RoleVo vo) {
        ValidationUtil.validation(vo, ValidationUtil.del.class);
        boolean save = bRoleService.removeRole(vo.getId());
        return new ResultData<>(save);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultData<Boolean> update(@RequestBody RoleVo vo) {
        ValidationUtil.validation(vo);
        boolean b = bRoleService.updateRole(PageUtil.transformation(vo, Role::new), vo.getTree());
        return new ResultData<>(b);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResultData<PageData<Role>> list(@RequestBody PageVo<RoleVo> vo) {
        RoleVo data = vo.getData();
        PageData<Role> rolePageData = bRoleService.queryPage(PageUtil.transformation(data, Role::new), vo.getPage(), vo.getSize());
        return new ResultData<>(rolePageData);
    }

    @RequestMapping(value = "/getRoles", method = RequestMethod.GET)
    public ResultData<List<Role>> getRoles() {
        val list = bRoleService.list();
        return new ResultData<>(list);
    }


    @RequestMapping(value = "/getProsByRole", method = RequestMethod.POST)
    public ResultData<List<Long>> getProsByRole(@RequestBody RoleVo vo) {
        val list = bRoleService.getProById(vo.getId());
        return new ResultData<>(list);
    }



    @RequestMapping(value = "/services", method = RequestMethod.GET)
    public ResultData<List<RolePro>> services() {
        val services = bRoleService.services();
        return new ResultData<>(services);
    }



}
